package framework;

import java.util.Arrays;

import javax.naming.NamingException;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public abstract class BrowserFactory {

    public enum BrowserType {
    CHROME("googleChrome"), FIREFOX("firefox"), INTERNET_EXPLORER("iexplore");

    private String val;

    BrowserType(String name) {
        val = name;
    }

    public String toString() {
        return val;
    }
    }
    //Implementation of the 'Factory method' pattern
    public static RemoteWebDriver setUp(BrowserType type) {
    RemoteWebDriver driver = null;
    switch (type) {
    case CHROME:
        DesiredCapabilities cp1 = DesiredCapabilities.chrome();
        //Set property, that browser will start maximized
        cp1.setCapability("chrome.switches",
                Arrays.asList("--disable-popup-blocking", "--start-maximized"));
        System.setProperty("webdriver.chrome.driver",
                "chromedriver.exe");
        System.setProperty("webdriver.chrome.driver",
                "src\\test\\resources\\chromedriver.exe");
        try {
        driver = new ChromeDriver(cp1);
        } catch (IllegalStateException e) {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        driver = new ChromeDriver(cp1);
        }
        break;
    case FIREFOX:
        driver = new FirefoxDriver();
        break;
    case INTERNET_EXPLORER:
        DesiredCapabilities cp = DesiredCapabilities.internetExplorer();
        //Default IE capability (needed for Webdriver's correct work)
        cp.setCapability(
                InternetExplorerDriver
                    .INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        driver = new InternetExplorerDriver(cp);
        break;
    }
    return driver;
    }

    public static RemoteWebDriver setUp(String type) throws NamingException {
    for (BrowserType t : BrowserType.values()) {
        if (t.toString().equalsIgnoreCase(type)) {
        return setUp(t);
        }
    }
    throw new NamingException(
            "browser name is wrong."
          + "Acceptable names:\ngoogleChrome\nfirefox\niexplore");
    }
}
